﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using VideoClipNameHelper;

namespace ImportDb
{
    class VideoFilePlus
    {
        //public string fNameExt;
        //public VideoClipNameHelper.VideoFile vf;
    }

    class Program
    {
        //static VideoClipNameHelper.ConfigPaths cp = new VideoClipNameHelper.ConfigPaths();
        //static List<VideoClipNameHelper.VideoFile> sourceVideoFiles;
        //static List<string> errList = new List<string>();

        static void Main(string[] args)
        {
            //
            //
            string ddfn = "@DumpDb.txt";
            Console.WriteLine("-- ReadAllLines(ddfn");
            string[] dd = System.IO.File.ReadAllLines(ddfn);
            //
            //
            using (var context = new UsageStatisticContext())
            {
                //context.UsageStatistics.ExecuteSqlCommand("TRUNCATE TABLE [UsageStatistics]");
                //var all = from c in context.UsageStatistics select c;
                //context.UsageStatistics.RemoveRange(all);
                //context.SaveChanges();
                foreach (string l in dd)
                {
                    string s = l.Trim();
                    if ("" == s) continue;
                    if (s.StartsWith("#")) continue;
                    //
                    string[] sp = s.Split(new[] { '\t' });
                    if (sp.Length < 5)
                    {
                        continue;
                    }
                    //VideoClipNameHelper.VideoClipData vc = new VideoClipNameHelper.VideoClipData();
                    //VideoClipNameHelper.VideoClipData.PARSE_RESULT pr = VideoClipNameHelper.VideoClipData.ParseWeak(sp[0], vc, false);
                    //if (pr != VideoClipNameHelper.VideoClipData.PARSE_RESULT.Invlid)
                    //{
                    //
                    //string nn = vc.Concat(true);
                    //UsageStatistic found = context.UsageStatistics.FirstOrDefault<UsageStatistic>(y => 0 == string.Compare(y.clipName, nn, true));
                    //if (null == found)
                    //if (true)
                    //{
                        //
                        //
                        UsageStatistic uc = new UsageStatistic();
                        uc.clipName = sp[0];
                        uc.nPlayed = int.Parse(sp[1]);
                        uc.nSkipped = int.Parse(sp[2]);
                        //
                        //uc.isDeleted = bool.Parse(sp[3]);
                        int deleted = int.Parse(sp[3]);
                        uc.isDeleted = deleted > 0;
                        //
                        uc.isLiked = bool.Parse(sp[4]);
                        context.UsageStatistics.Add(uc);
                        int k=context.SaveChanges();
                        Console.WriteLine(k + "+++ " + s);
                    //}
                    //else
                    //{
                    //Console.WriteLine("\tdubel" + sp[0]);

                    //}
                    ///else
                    //{
                    //Console.WriteLine("\t--- PARSE_RESULT.Invlid");
                }//foreach
                //
            //
            Console.WriteLine("-------------------");

        }//using
        }//Main

        //using (var context = new UsageStatisticContext())
        //{
        //var toDelete = context.UsageStatistics.Where(s => s.nDeleted != 0).ToArray();
        //foreach (var x in toDelete)
        //{
        //context.UsageStatistics.Remove(x);
        //}
        //}
        //
        //string clipName = "";
        //VideoClipNameHelper.CDBHandler.UpdateData(clipName);
        //Console.WriteLine("--------------------------------");
        //VideoClipNameHelper.CDBHandler.DumpDb();
        //Console.WriteLine("--------------------------------");
    //}
    /*
    public static async Task<bool> UpdateData(string clipName, bool updatenPlayed = false, bool updatenSkipped = false, bool isDeleted = false, bool isLiked = false)
    {
        //
        try
        {
            using (var context = new UsageStatisticContext())
            {
                foreach (var x in context.UsageStatistics)
                {
                    //x.
                }
                UsageStatistic found = context.UsageStatistics.FirstOrDefault<UsageStatistic>(x => 0 == string.Compare(x.clipName, clipName, true));
                if (null == found)
                {
                    UsageStatistic uc = new UsageStatistic();
                    uc.clipName = clipName;
                    uc.nPlayed = updatenPlayed ? 1 : 0;
                    uc.nSkipped = updatenSkipped ? 1 : 0;
                    uc.isDeleted = isDeleted;
                    uc.isLiked = isLiked;
                    context.UsageStatistics.Add(uc);
                }
                else
                {
                    found.nPlayed = updatenPlayed ? found.nPlayed + 1 : found.nPlayed;
                    found.nSkipped = updatenSkipped ? found.nSkipped + 1 : found.nSkipped;
                    found.isDeleted = isDeleted;
                    found.isLiked = isLiked;
                }
                await context.SaveChangesAsync();
            }
        }
        catch
        {
            //Console.WriteLine("DB Failed");
        }
        //
        return true;
    }
    */
}//class
}//namspace

    //=========================================================================================================================================
    /*
        //Verve, The - Bitter Sweet Symphony.lnk	1	1	0
        string ddfn = "@DumpDb.txt";
        //27.11.2015 16:27:30 [D:\Video_AVI_Music\_Clips all\Reo Speedwagon - Can't Fight This Feeling(1984)[640x480+124OGG].mkv]
        string lifn = "likes.txt";
        //
        Console.WriteLine("-- ReadConfig");
        VideoClipNameHelper.ConfigData.ReadConfig(cp);
        Console.WriteLine("-- LoadClipsFromDisk");
        VideoClipNameHelper.VideoFile.LoadClipsFromDisk(cp.SourceDirs, ref sourceVideoFiles, errList);
        Console.WriteLine("-- ReadAllLines(ddfn");
        string[] dd = System.IO.File.ReadAllLines(ddfn);
        Console.WriteLine("-- ReadAllLines(lifn");
        string[] li = System.IO.File.ReadAllLines(lifn);
        Console.WriteLine("-- End");
        //
        foreach(string s in li)
        {
            string s1 = s.Trim();
            if ("" == s1) continue;
            s1 = s1.Substring(22);
            s1 = s1.Substring(0, s1.Length - 1);
            s1 = System.IO.Path.GetFileName(s1);
            //
            Console.WriteLine(">"+s1+"<");
            //
            VideoClipNameHelper.CDBHandler.UpdateLike(s1, true);
            //
            //
        foreach(string s in dd)
        {
            string s1 = s.Trim();
            if ("" == s1) continue;
            //s1 = s1.Substring(4);
            //
            string[] sp = s1.Split(new []{ '\t' });
            if (sp.Length < 4) continue;
            //
            VideoClipNameHelper.VideoClipData vc = new VideoClipNameHelper.VideoClipData();
            VideoClipNameHelper.VideoClipData.PARSE_RESULT pr = VideoClipNameHelper.VideoClipData.ParseWeak(sp[0], vc, false);
            if(pr != VideoClipNameHelper.VideoClipData.PARSE_RESULT.Invlid)
            {
                //
                //
                bool isFound = false;
                Console.WriteLine("-- File:"+s1);
                foreach (VideoClipNameHelper.VideoFile vf in sourceVideoFiles)
                {
                    if(vf.vcd.IsEqWeak(vc))
                    {
                        Console.WriteLine("-- Found");
                        isFound = true;
                        VideoClipNameHelper.CDBHandler.WriteRecord(vf.FileName(), int.Parse(sp[1]), int.Parse(sp[2]), int.Parse(sp[3]), false);
                        if(VideoClipNameHelper.CDBHandler.IsFailed)
                        {
                            Console.WriteLine("-- DB FAILED!");
                            return;
                        }
                        break;
                    }
                }
                if(!isFound)
                {
                    Console.WriteLine("-- NOT Found");
                }
                //
                //
            }
            else
            {
                Console.WriteLine("-- PARSE_RESULT.Invlid");
            }
            //
            //
        }


     */
    //===========================================================================================================================
